/**
 * ClassName 部门管理控制器
 */
Ext.define("login.controller.LoginController",{
	extend:'Ext.app.Controller',
	init: function(){
		this.control({
			//设定列表添加按钮的事件
		'loginform button[action = login]' : {
			click:'login',
		 },
			//登录页的注册按钮
		'loginform button[action = toRegister]' : {
			click:'toRegister',
			},
			//注册页注册按钮
		'registerform button[action = register]' : {
			click:'register',
			},
		//注册页登录按钮
		'registerform button[action = toLogin]' : {
			click:'toLogin',
			},
		//注册页用户名文本框
		'registerform textfield[id=username]' : {
			blur:'validateUser',
			},
		})
		
	},
	
	//登录功能
	login:function(btn, e, eOpts){
		console.log(btn.ownerCt.ownerCt.getValues());
		//alert(win);
		var data = btn.ownerCt.ownerCt.getValues();
		Ext.Ajax.request({
			url:ROOT+'/index.php/Home/Login/login',
			params:data,
			method:'post',
			timeout:2000,
			success:function(response,opts){
				
				var response=response.responseText;
				//console.log(response);
				if(response==1){
					window.location.href = ROOT;
				}else{
					Ext.Msg.alert('登录信息','用户名密码错误');
				}
			}
		})
	},
	
	//显示注册页面
	toRegister : function(btn, e, eOpts){
		//var panel = btn.ownerCt.ownerCt;
		var winLog = btn.ownerCt.ownerCt.ownerCt;//登录窗口
		
		var winReg = Ext.widget('registerform');//注册窗口
		winLog.destroy( );//隐藏登录窗口

		winReg.show();//显示注册窗口		
	},
	
	//提交注册表单
	register : function(btn, e, eOpts){
		//alert();
		var form = btn.up("form");
		var data = form.getValues();
		
		Ext.Ajax.request({
			url:ROOT+'/index.php/Home/Register/register',
			params:data,
			method:'post',
			timeout:2000,
			success:function(response,opts){
				
				var response=response.responseText;
				//console.log(response);
				if(response=='注册成功'){
					Ext.Msg.alert('注册结果',response);
					window.location.href = ROOT+'/index.php/Home/Login';
				}else{
					Ext.Msg.alert('注册结果',response);
				}
			}
		})
	},	
	
	//显示登录页面
	toLogin : function(btn, e, eOpts){
		//var panel = btn.ownerCt.ownerCt;
		var winReg = btn.ownerCt.ownerCt.ownerCt;//注册窗口
		
		var winLog = Ext.widget('loginform');//登录窗口
		winReg.destroy( );//隐藏注册窗口

		winLog.show();//显示登录窗口		
	},
	
	//验证用户名是否存在
	validateUser : function(username, event, eOpt){
		console.log(username);
		console.log(event);
		console.log(eOpt);
		var value = username.value;
		var data = {'username':value};
		Ext.Ajax.request({
			url:ROOT+'/index.php/Home/Register/validateUser',
			params:data,
			method:'post',
			timeout:2000,
			success:function(response,opts){
				
				var response=response.responseText;
				console.log(response);
				if(response!=1){
					username.markInvalid('该用户名已被使用');//错误提示信息
					return false;					
				}
			}
		})		
	},
	views:[
	       'LoginForm',
	       'RegisterForm'
	       ],
	stores:[
	      /*  'MenuStore',
	        'StaffStore'*/
	       ],
	models:[/*'StaffModel'*/],
})